$(document).ready(
    function () {

        /**
         * 获取项目根路径
         *
         * @returns {string} 返回路径地址
         */
        function getRootPath() {
            return "http://127.0.0.1";
        }

        // 获取主机地址之后的目录
        var curWwwPath = window.document.location.href;

        /**
         * 截取url
         * @returns {Object}
         * @constructor
         */
        function GetRequest() {
            var url = location.search; //获取url中"?"符后的字串
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                }
            }
            return theRequest;
        }


        var Request = new Object();
        Request = GetRequest();


        //提交添加按钮
        $("#addContentButton").click(function () {
            $("#addContentFrom").attr("action", getRootPath() + "/manager/increaseArticle.do");
            $("#oneClass").attr("value", Request["one"]);
            $("#twoClass").attr("value", Request["two"]);
            $("#addContentFrom").submit();
        });

        //修改提交按钮
        $("#updateContentButton").click(function () {
            $("#updateContentFrom").attr("action", getRootPath() + "/manager/renewalArticle.do");
            $("#resourceId").val(Request["id"]);
            $("#updateContentFrom").submit();
        });

        //公用页面添加按钮
        $("#communalContentId").click(function () {
            if (curWwwPath.indexOf("one=Copyright&two=Context") < 0) {
                $("#communalContentId").attr("href", "addContent.html?one=" + Request["one"] + "&two=" + Request["two"]);
            }
        });

        //添加资源按钮
        $("#addResourceButton").click(function () {
            $("#addResourceFrom").attr("action", getRootPath() + "/manager/addResource.do")
            $("#artId").val(Request["artId"]);
            $("#addResourceFrom").submit();
        });

        //我们团队修改按钮
        $("#weUpdateContextButton").click(function () {
            $("#updateWeContentFrom").attr("action", getRootPath() + "/manager/renewalArticle.do");
            $("#updateWeContentFrom").submit();
        });

        //我们的能力修改按钮
        $("#abilityUpdateButton").click(function () {
            $("#abilityFrom").attr("action", getRootPath() + "/manager/renewalArticle.do");
            $("#abilityFrom").submit();
        });

        //我们的合作伙伴修改按钮
        $("#partnerUpdateButton").click(function () {
            $("#partnerFrom").attr("action", getRootPath() + "/manager/renewalArticle.do");
            $("#partnerFrom").submit();
        });

        //我们的能力修改按钮
        $("#honorUpdateButton").click(function () {
            $("#honorFrom").attr("action", getRootPath() + "/manager/renewalArticle.do");
            $("#honorFrom").submit();
        });

        //资源修改按钮
        $("#updateResourceButton").click(function () {
            $("#updateResourceFrom").attr("action", getRootPath() + "/manager/updateResource.do");
            $("#updateResourceId").val(Request["id"]);
            $("#updateResourceFrom").submit();
        });


        //后台首页
        function getIndex(thisClickValue) {
            $.ajax({
                type: "POST",
                async: false,
                url: getRootPath() + "/manager/listLink.do",
                data: "className=" + thisClickValue,
                success: function (data) {
                    //会话超时判断
                    if (data.length == 73) {
                        alert("会话已超时");
                        document.location = 'signin.html';
                    }

                    $("#clickLinkList").empty();
                    $.each(data.data, function (key, clickLink) {
                        $("#clickLinkList").append('<tr><td>' + clickLink.id + '</td>' +
                            '<td>' + clickLink.titleName + '</td>' +
                            '<td>' + clickLink.clickPath + '</td>' +
                            '<td>' + clickLink.isUse + '</td>' +
                            '<td><div class="btn-group"><a data-toggle="dropdown" class="dropdown-toggle"><i class="fa fa-cog"></i></a> <ul role="menu" class="dropdown-menu pull-right"><li><a href=addClickLink.html?id=' + clickLink.id + '>修改</a></li><li><a href=delLink.do?cid=' + clickLink.id + '>删除</a></li></ul></div></td></tr>'
                        );
                    });

                }
            });

        }

        //定义一个变量，存入点击表格值
        var thisClickValue = '';
        $("#tableUl li a").click(function () {
            thisClickValue = $(this).children('strong').text();
            getIndex(thisClickValue);
        });
        //后台首页获取
        if (curWwwPath.indexOf("/index.html") > 0) {
            getIndex('电视');
        }

        if (curWwwPath.indexOf("/addClickLink.html?id=") > 0) {
            $.ajax({
                type: "POST",
                async: false,
                url: getRootPath() + "/manager/quLink.do",
                data: "id=" + Request["id"],
                success: function (data) {
                    //会话超时判断
                    if (data.length == 73) {
                        alert("会话已超时");
                        document.location = 'signin.html';
                    }


                    $("#titleName").val(data.data.titleName);
                    $("#clickPath").val(data.data.clickPath);
                    $("#className").val(data.data.className);
                    $("#isUse").val(data.data.isUse);
                }
            });
        }


        //点击添加链接按钮
        $("#addClickButton").click(function () {
            if (curWwwPath.indexOf("/addClickLink.html?id=") > 0) {
                $("#addClickFrom").attr("action", getRootPath() + "/manager/upLink.do")
                $("#clickId").attr("value", Request["id"]);
                $("#addClickFrom").submit();
            } else {
                $("#addClickFrom").submit();
            }
        });

        //修改用户密码
        $("#updatePassword").click(function () {
            var password = $("#password").val();
            var password1 = $("#password1").val();

            if (password == password1) {
                $("#updatePasswordForm").submit();
            } else {
                alert("确认密码不正确");
                password.empty();
                password1.empty();
            }
        });
    });
